// ClientCount.cpp: implementation of the CClientCount class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RMSTranslator.h"
#include "ClientCount.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CClientCount::CClientCount() : m_dwClientCount(0)
{
	m_bInitClientCount = FALSE;
}

CClientCount::~CClientCount()
{

}

HANDLE CClientCount::InitClientCount()
{
	m_dwClientCount = 0;

	InitializeCriticalSection(&m_criticalClients);

	m_hNoClients = CreateEvent(NULL, TRUE, TRUE, NULL);

	m_bInitClientCount = TRUE;

	return m_hNoClients;
}

void CClientCount::DeleteClientCount()
{
	DeleteCriticalSection(&m_criticalClients);
	m_bInitClientCount = FALSE;

	CloseHandle(m_hNoClients);
}

void CClientCount::IncrementClientCount()
{
	if(m_bInitClientCount)
	{
		EnterCriticalSection(&m_criticalClients);
		m_dwClientCount++;
		LeaveCriticalSection(&m_criticalClients);
		ResetEvent(m_hNoClients);
	}
}

void CClientCount::DecrementClientCount()
{
	if(m_bInitClientCount)
	{
		EnterCriticalSection(&m_criticalClients);
		if (m_dwClientCount > 0)
			m_dwClientCount--;
		LeaveCriticalSection(&m_criticalClients);
		if (m_dwClientCount < 1)
			SetEvent(m_hNoClients);
	}
}

DWORD CClientCount::GetClientCount()
{
	DWORD result = 0;
	if(m_bInitClientCount)
	{
		EnterCriticalSection(&m_criticalClients);
		result = m_dwClientCount;
		LeaveCriticalSection(&m_criticalClients);
	}
	return result;
}

void CClientCount::SetClientCount(DWORD dwCount)
{
	if(m_bInitClientCount)
	{
		EnterCriticalSection(&m_criticalClients);
		m_dwClientCount = dwCount;
		LeaveCriticalSection(&m_criticalClients);
		if (m_dwClientCount < 1)
			SetEvent(m_hNoClients);
	}
}
